package com.qiguliuxing.dts.db.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.qiguliuxing.dts.db.dao.DtsGrouponMapper;
import com.qiguliuxing.dts.db.dao.ex.GrouponMapperEx;
import com.qiguliuxing.dts.db.domain.DtsGroupon;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DtsGrouponService {
	@Resource
	private DtsGrouponMapper mapper;
	@Resource
	private GrouponMapperEx grouponMapperEx;

	/**
	 * 获取用户发起的团购记录
	 *
	 * @param userId
	 * @return
	 */
	public List<DtsGroupon> queryMyGroupon(Integer userId) {
		QueryWrapper<DtsGroupon> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsGroupon::getUserId, userId);
		queryWrapper.lambda().eq(DtsGroupon::getCreatorUserId, userId);
		queryWrapper.lambda().eq(DtsGroupon::getGrouponId, 0);
		queryWrapper.lambda().eq(DtsGroupon::getDeleted, false);
		queryWrapper.lambda().eq(DtsGroupon::getPayed, true);
		queryWrapper.lambda().orderByDesc(DtsGroupon::getAddTime);
		return mapper.selectList(queryWrapper);
	}

	/**
	 * 获取用户参与的团购记录
	 *
	 * @param userId
	 * @return
	 */
	public List<DtsGroupon> queryMyJoinGroupon(Integer userId) {
		QueryWrapper<DtsGroupon> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsGroupon::getUserId, userId);
		queryWrapper.lambda().eq(DtsGroupon::getCreatorUserId, userId);
		// 不等于
		queryWrapper.lambda().ne(DtsGroupon::getGrouponId, 0);
		queryWrapper.lambda().eq(DtsGroupon::getDeleted, false);
		queryWrapper.lambda().eq(DtsGroupon::getPayed, true);
		queryWrapper.lambda().orderByDesc(DtsGroupon::getAddTime);
		return mapper.selectList(queryWrapper);
	}

	/**
	 * 根据OrderId查询团购记录
	 *
	 * @param orderId
	 * @return
	 */
	public DtsGroupon queryByOrderId(Integer orderId) {
		QueryWrapper<DtsGroupon> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsGroupon::getOrderId, orderId);
		queryWrapper.lambda().eq(DtsGroupon::getDeleted, false);
		return mapper.selectOne(queryWrapper);
	}

	/**
	 * 获取某个团购活动参与的记录
	 *
	 * @param id
	 * @return
	 */
	public List<DtsGroupon> queryJoinRecord(Integer id) {
		QueryWrapper<DtsGroupon> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsGroupon::getGrouponId, id);
		queryWrapper.lambda().eq(DtsGroupon::getDeleted, false);
		queryWrapper.lambda().eq(DtsGroupon::getPayed, true);
		queryWrapper.lambda().orderByDesc(DtsGroupon::getAddTime);
		return mapper.selectList(queryWrapper);
	}

	/**
	 * 根据ID查询记录
	 *
	 * @param id
	 * @return
	 */
	public DtsGroupon queryById(Integer id) {
		QueryWrapper<DtsGroupon> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsGroupon::getId, id);
		queryWrapper.lambda().eq(DtsGroupon::getDeleted, false);
		queryWrapper.lambda().eq(DtsGroupon::getPayed, true);
		return mapper.selectOne(queryWrapper);
	}

	/**
	 * 返回某个发起的团购参与人数
	 *
	 * @param grouponId
	 * @return
	 */
	public int countGroupon(Integer grouponId) {
		QueryWrapper<DtsGroupon> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(DtsGroupon::getGrouponId, grouponId);
		queryWrapper.lambda().eq(DtsGroupon::getDeleted, false);
		queryWrapper.lambda().eq(DtsGroupon::getPayed, true);
		return mapper.selectCount(queryWrapper).intValue();
	}

	public int updateById(DtsGroupon groupon) {
		groupon.setUpdateTime(LocalDateTime.now());
		return mapper.updateById(groupon);
	}

	/**
	 * 创建或参与一个团购
	 *
	 * @param groupon
	 * @return
	 */
	public int createGroupon(DtsGroupon groupon) {
		groupon.setAddTime(LocalDateTime.now());
		groupon.setUpdateTime(LocalDateTime.now());
		return mapper.insert(groupon);
	}

	/**
	 * 查询所有发起的团购记录
	 *
	 * @param rulesId
	 * @param page
	 * @param size
	 * @param sort
	 * @param order
	 * @return
	 */
	public List<DtsGroupon> querySelective(String rulesId, Integer page, Integer size, String sort, String order) {
		QueryWrapper<DtsGroupon> queryWrapper = new QueryWrapper<>();
		if (StrUtil.isNotEmpty(rulesId)) {
			queryWrapper.lambda().eq(DtsGroupon::getRulesId, rulesId);
		}
		queryWrapper.lambda().eq(DtsGroupon::getGrouponId, 0);
		queryWrapper.lambda().eq(DtsGroupon::getDeleted, false);
		queryWrapper.lambda().eq(DtsGroupon::getPayed, true);
		IPage<DtsGroupon> iPage = new Page<>(page, size);

		return mapper.selectPage(iPage, queryWrapper).getRecords();
	}

	/**
	 * 根据品牌入驻店铺获取对应的团购数据
	 * @param brandIds
	 * @param rulesId
	 * @param page
	 * @param sort
	 * @param order
	 * @return
	 */
	public List<DtsGroupon> queryBrandGroupons(List<Integer> brandIds, String rulesId, Integer page, Integer size,
			String sort, String order) {
		String orderBySql = null;
		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			orderBySql = "o."+sort + " " + order;
		}
		
		String brandIdsSql = null;
		if (brandIds != null) {
			brandIdsSql = "";
			for (Integer brandId : brandIds) {
				brandIdsSql += "," + brandId;
			}
			brandIdsSql = " and g.brand_id in (" + brandIdsSql.substring(1) + ") ";
		}
		
		PageHelper.startPage(page, size);
		
		return grouponMapperEx.queryBrandGroupons(rulesId,orderBySql,brandIdsSql);
	}
}
